package tech.eyao.hs60x.user.controller;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import tech.eyao.hs60x.annotation.Admin;
import tech.eyao.hs60x.dto.BaseResult;
import tech.eyao.hs60x.model.domain.AccountDO;
import tech.eyao.hs60x.service.AccountRelationService;
import tech.eyao.hs60x.utils.TokenUtils;

/**
 * 绑定
 * 
 * @author Administrator
 */
@RestController
@Api("绑定手表相关")
@RequestMapping("/watch")
public class BindWatchController {

    @Autowired
    private AccountRelationService accountRelationService;

    /**
     * 绑定watch
     * 
     * @param wacthAccountId
     * @return TODO 区分APP手动/自动绑定watch类型区分，手表无账户的时候，给予自动注册，并通知管理员
     */
    @PostMapping("/bind.do")
    @ApiOperation(value = "绑定手表")
    public BaseResult bindWatch(@ApiParam(value = "绑定者关系名称") @RequestParam(value="relationName",defaultValue="")String relationName, 
    							@ApiParam(value = "手机") @RequestParam(value="phone",defaultValue="")String phone,
                                @ApiParam(value = "性别") @RequestParam(value="sex",defaultValue="0")Integer sex, 
                                @ApiParam(value = "宝贝昵称") @RequestParam(value="nickName",defaultValue="")String nickName,
                                @ApiParam(value = "身高") @RequestParam(value="height",defaultValue="0")int height, 
                                @ApiParam(value = "体重") @RequestParam(value="weight",defaultValue="0")int weight,
                                @ApiParam(value = "生日") @RequestParam(value="birthday",defaultValue="0")long birthday, 
                                @ApiParam(value = "设备ID") @RequestParam(value="deviceId",defaultValue="")String deviceId,
                                @ApiParam(value = "绑定类型 1:扫描二维码2：手动") @RequestParam(value="bindType",defaultValue="1")Integer bindType,
                                @ApiParam(value = "设备头像") @RequestParam(value="headerUrl",defaultValue="")String headerUrl,
                                @ApiParam(value = "申请绑定备注") @RequestParam(value="bindTips",defaultValue="")String bindTips) {
        //参数合法性校验
        /*if (StringUtils.isBlank(phone) || StringUtils.isBlank(nickName)) {
            return BaseResult.isFailure(ExceptionCodeEnum.ERROR_PARAMERROR.getCode(),
                    ExceptionCodeEnum.ERROR_PARAMERROR.getMessage());
        }*/
        AccountDO deviceAccountDO = new AccountDO();
        if(StringUtils.isNotEmpty(phone)) {
        	deviceAccountDO.setPhone(phone);
        }
        if(null != sex) {
        	deviceAccountDO.setSex(sex);
        }
        if(StringUtils.isNotEmpty(nickName)) {
        	deviceAccountDO.setNickName(nickName);
        }
        if(height > 0) {
        	deviceAccountDO.setHeight(height);
        }
        if(weight > 0) {
        	deviceAccountDO.setWeight(weight);
        }
        if(birthday > 0) {
        	deviceAccountDO.setBirthday(new LocalDateTime(birthday).toDate());
        }
        if(StringUtils.isNotEmpty(deviceId)) {
        	deviceAccountDO.setDeviceId(deviceId);
        }
        if(StringUtils.isNotEmpty(headerUrl)) {
        	deviceAccountDO.setHeadUrl(headerUrl);
        }
        if(null != bindType) {
        	deviceAccountDO.setAccountType(bindType);
        }
        return accountRelationService.bindWatch(relationName, TokenUtils.getUserId(), bindType,bindTips,deviceAccountDO);
    }
    
    
    /**
     * 解绑watch
     * 
     * @param wacthAccountId
     * @return
     */
    @PostMapping("/unbind.do")
    @ApiOperation(value = "解绑手表")
    public BaseResult unbind(@ApiParam(value = "手表账户ID") Long watchId) {
        return accountRelationService.unbindWatch(watchId, TokenUtils.getUserId());
    }

    @Admin
    @ApiOperation(value = "转让手表管理员")
    @PostMapping("/setWatchAdmin.do")
    public BaseResult setWatchAdmin(@ApiParam("手表ID") Long watchId, @ApiParam("对方账户ID") Long accountId ,
                                    @ApiParam("对方昵称") @RequestParam(value="nickName",defaultValue="")String nickName) {
        return accountRelationService.setWatchAdmin(TokenUtils.getUserId(), watchId, accountId, nickName);
    }
}
